package action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import service.service_admin.Service_Admin;
import service.service_public.Service_Public;
import service.service_search.Service_Search;
import tools.Convert;
import tools.InterfaceForResultSetToList;
import tools.InterfaceSeaAreaIceList;
import tools.InterfaceStationIceList;
import tools.PageBeanPlus;
import view.ViewOc_Station;
import view.ViewOc_Station_ICE;
import entitybean.l_sequence.EntityBeanL_Sequence;
import entitybean.oc_boat.EntityBeanOc_Boat;
import entitybean.oc_boat_ice.EntityBeanOc_Boat_Ice;
import entitybean.oc_sea_area.EntityBeanOc_Sea_Area;
import entitybean.oc_sea_area_ice.EntityBeanOc_Sea_Area_Ice;
import entitybean.oc_station.EntityBeanOc_Station;
import entitybean.oc_station_ice.EntityBeanOc_Station_Ice;
import entitybean.oc_voyage.EnitityBeanOc_Voyage;
import entitybean.oc_voyage_ice.EnitityBeanOc_Voyage_Ice;
import form.PropertyIceForm;
import form.SeaAreaIceForm;
import form.StationForm;
import form.StationIcForm;

public class Action_Oc_Station_Ice extends DispatchAction {
	private Service_Admin service_admin;
	private Service_Public service_public;
	private Service_Search service_search;

	public Service_Admin getService_admin() {
		return service_admin;
	}

	public void setService_admin(Service_Admin service_admin) {
		this.service_admin = service_admin;
	}

	public Service_Public getService_public() {
		return service_public;
	}

	public void setService_public(Service_Public service_public) {
		this.service_public = service_public;
	}

	public Service_Search getService_search() {
		return service_search;
	}

	public void setService_search(Service_Search service_search) {
		this.service_search = service_search;
	}

	public ActionForward admin_init_oc_station_ice(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		StationIcForm actionForm = (StationIcForm) form;
		System.out.println("初始化海洋环境科考船");
		String sql = "select ID,STATION_NAME,LONGITUDE,DIMENSIONALITY,TERRITORIAL_WATERS,WORK_TIME,DEPTH,OC_VOYAGE_ID,"
				+ "(SELECT OC_SEA_AREA from OC_SEA_AREA_ICE where id=TERRITORIAL_WATERS)OC_SEA_AREA,"
				+ "(SELECT VOYAGE_NAME FROM OC_VOYAGE_ICE WHERE ID=OC_VOYAGE_ID)VOYAGE_NAME from OC_STATION_ICE where DEL='OFF' ORDER BY id DESC";
		PageBeanPlus pagebeanplus = new PageBeanPlus("oracle", "sql", sql,
				Integer.valueOf(actionForm.getRowcountperpage()), Integer
						.valueOf(actionForm.getCurrentpage()), "loadrichtext()");
		InterfaceStationIceList interfaceforresultsettolist = new InterfaceStationIceList();
		List<ViewOc_Station_ICE> list = interfaceforresultsettolist
				.ViewOc_News(pagebeanplus.getFormatqueryresultset());
		request.setAttribute("pagebeanplus", pagebeanplus);// 设置传递到页面的数据
		request.setAttribute("list", list);
		request.setAttribute("stationform", actionForm);
		System.out.println("list.size()" + list.size());
		return mapping.findForward("success");
	}

	public ActionForward admin_update_init_oc_station_ice(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		StationIcForm actionForm = (StationIcForm) form;
		System.out.println("封装表格");
		List<EntityBeanOc_Sea_Area_Ice> list_sea_area = this.service_search
				.Find_EntityBeanOc_Sea_Area_Ice();
		List<EntityBeanOc_Boat_Ice> list_boat = this.service_search
				.Find_EntityBeanOc_Boat_Ice();
		String id = actionForm.getId();
		EntityBeanOc_Station_Ice e = new EntityBeanOc_Station_Ice();
		e = this.service_search.Find_EntityBeanOc_Station_Ice(Integer.valueOf(id));

		String ns = "";
		String ew = "";
		String a[] = e.getLongitude().split(" ");
		String b[] = e.getDimensionality().split(" ");
		ew = a[0];
		ns = b[0];
		e.setLongitude(a[1]);
		e.setDimensionality(b[1]);
		actionForm.setEw(ew);
		actionForm.setNs(ns);
		// 配置相应的数据航次数据
		// e.getOc_voyage_id();//反查航次所属的科考船
		EnitityBeanOc_Voyage_Ice voy = this.service_search.Find_EnitityBeanOc_Voyage_Ice(e.getOc_voyage_id());
         EntityBeanOc_Boat_Ice boat = this.service_search.Find_EntityBeanOc_Boat_Ice(voy.getBoatid());
		List<EnitityBeanOc_Voyage_Ice> voyage_list = this.service_search.Find_Oc_Voyage_Ice_By_boatid(boat.getId());

		request.setAttribute("boat", boat);
		request.setAttribute("voyage_list", voyage_list);

		request.setAttribute("e", e);
		request.setAttribute("list_sea_area", list_sea_area);
		request.setAttribute("list_boat", list_boat);
		request.setAttribute("stationform", actionForm);
		return mapping.findForward("success");
	}

	public ActionForward admin_add_oc_station_ice(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		List<EntityBeanOc_Sea_Area_Ice> list_sea_area = this.service_search
				.Find_EntityBeanOc_Sea_Area_Ice();
		List<EntityBeanOc_Boat_Ice> list_boat = this.service_search
				.Find_EntityBeanOc_Boat_Ice();
		request.setAttribute("list_sea_area", list_sea_area);
		request.setAttribute("list_boat", list_boat);
		return mapping.findForward("success");
	}

	public ActionForward admin_insert_oc_station_ice(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		StationIcForm stationform = (StationIcForm) form;
		String ctd = stationform.getCtd();
		Double depth = stationform.getDepth();
		String dimensionality = stationform.getDimensionality();
		String longitude = stationform.getLongitude();
		Integer oc_voyage_id = stationform.getOc_voyage_id();
		Integer territorial_waters = stationform.getTerritorial_waters();
		String work_time = stationform.getWork_time();
		String station_name = stationform.getStation_name();
		EntityBeanOc_Station_Ice e = new EntityBeanOc_Station_Ice();
		e.setCtd(ctd);
		e.setDel("OFF");
		e.setDepth(depth);
		e.setDimensionality(dimensionality);
		//work_time 转数据
		work_time=work_time+" 00:00:00";
		Convert convert=new Convert();
		e.setWork_time(convert.ConvertStringToDate(work_time));
		EntityBeanL_Sequence l = this.service_public.Find_L_Sequence_By_seq_Name("SEQ_OC_STATION_ICE");
		Integer id = l.getSeq_Value() + 1;
		l.setSeq_Value(id);

		e.setId(id);
		e.setLongitude(longitude);
		e.setOc_voyage_id(oc_voyage_id);
		e.setStation_name(station_name);
		e.setTerritorial_waters(territorial_waters);
		// work_time 转数据
		e.setWork_time(new Date());
		this.service_public.Update_L_Sequence(l);
		this.service_search.Insert_Oc_Station_Ice(e);
		System.out.println("初始化新闻模块");
		// 封装新闻查询的数据及表单
		StationIcForm goodForm = (StationIcForm) form;
		goodForm.setCurrentpage("1");
		goodForm.setCtd("");
		goodForm.setStation_name("");
		goodForm.setOver_time("");
		goodForm.setStart_time("");
		String sql = "select ID,STATION_NAME,LONGITUDE,DIMENSIONALITY,TERRITORIAL_WATERS,WORK_TIME,DEPTH,OC_VOYAGE_ID,"
				+ "(SELECT OC_SEA_AREA from OC_SEA_AREA_ICE where id=TERRITORIAL_WATERS)OC_SEA_AREA,"
				+ "(SELECT VOYAGE_NAME FROM OC_VOYAGE_ICE WHERE ID=OC_VOYAGE_ID)VOYAGE_NAME from OC_STATION_ICE where DEL='OFF' ORDER BY id DESC";
		PageBeanPlus pagebeanplus = new PageBeanPlus("oracle", "sql", sql,
				Integer.valueOf(goodForm.getRowcountperpage()), Integer
						.valueOf(goodForm.getCurrentpage()), "loadrichtext()");
		InterfaceStationIceList interfaceforresultsettolist = new InterfaceStationIceList();
		List<ViewOc_Station_ICE> list = interfaceforresultsettolist
				.ViewOc_News(pagebeanplus.getFormatqueryresultset());
		request.setAttribute("pagebeanplus", pagebeanplus);// 设置传递到页面的数据
		request.setAttribute("list", list);
		request.setAttribute("stationform", goodForm);
		System.out.println("list.size()" + list.size());
		return mapping.findForward("success");
	}

	public ActionForward admin_delete_oc_station_ice(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		StationIcForm actionForm = (StationIcForm) form;
		String id = actionForm.getId();
		id = this.cut_tail(id);
		this.service_search.Delete_Oc_Station_Ice(id);

		String sql = "select ID,STATION_NAME,LONGITUDE,DIMENSIONALITY,TERRITORIAL_WATERS,WORK_TIME,DEPTH,OC_VOYAGE_ID,"
			+ "(SELECT OC_SEA_AREA from OC_SEA_AREA_ICE where id=TERRITORIAL_WATERS)OC_SEA_AREA,"
			+ "(SELECT VOYAGE_NAME FROM OC_VOYAGE_ICE WHERE ID=OC_VOYAGE_ID)VOYAGE_NAME from OC_STATION_ICE where DEL='OFF' ORDER BY id DESC";
	PageBeanPlus pagebeanplus = new PageBeanPlus("oracle", "sql", sql,
			Integer.valueOf(actionForm.getRowcountperpage()), Integer
					.valueOf(actionForm.getCurrentpage()), "loadrichtext()");
	InterfaceStationIceList interfaceforresultsettolist = new InterfaceStationIceList();
	List<ViewOc_Station_ICE> list = interfaceforresultsettolist
			.ViewOc_News(pagebeanplus.getFormatqueryresultset());
	request.setAttribute("pagebeanplus", pagebeanplus);// 设置传递到页面的数据
	request.setAttribute("list", list);
	request.setAttribute("stationform", actionForm);
	System.out.println("list.size()" + list.size());
		return mapping.findForward("success");
	}


	public ActionForward admin_update_oc_station_ice(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		StationIcForm stationform = (StationIcForm) form;
		String id=stationform.getId();
		String ctd=stationform.getCtd();
		Double depth=stationform.getDepth();
		String dimensionality=stationform.getDimensionality();
		String longitude=stationform.getLongitude();
		Integer oc_voyage_id=stationform.getOc_voyage_id();
		Integer territorial_waters=stationform.getTerritorial_waters();
		String work_time=stationform.getWork_time();
		String	station_name=stationform.getStation_name();
		EntityBeanOc_Station_Ice e=this.service_search.Find_EntityBeanOc_Station_Ice(Integer.valueOf(id));
		e.setCtd(ctd);
		e.setDepth(depth);
		e.setDimensionality(dimensionality);
		e.setLongitude(longitude);
		e.setOc_voyage_id(oc_voyage_id);
		e.setStation_name(station_name);
		e.setTerritorial_waters(territorial_waters);
		work_time=work_time+" 00:00:00";
		Convert convert=new Convert();
		e.setWork_time(convert.ConvertStringToDate(work_time));
		//work_time 转数据
		e.setWork_time(new Date());
		 this.service_search.Update_Oc_Station_Ice(e);
			StationIcForm actionForm = (StationIcForm) form;
			System.out.println("初始化海洋环境科考船");
			String sql = "select ID,STATION_NAME,LONGITUDE,DIMENSIONALITY,TERRITORIAL_WATERS,WORK_TIME,DEPTH,OC_VOYAGE_ID,"
					+ "(SELECT OC_SEA_AREA from OC_SEA_AREA_ICE where id=TERRITORIAL_WATERS)OC_SEA_AREA,"
					+ "(SELECT VOYAGE_NAME FROM OC_VOYAGE_ICE WHERE ID=OC_VOYAGE_ID)VOYAGE_NAME from OC_STATION_ICE where DEL='OFF' ORDER BY id DESC";
			PageBeanPlus pagebeanplus = new PageBeanPlus("oracle", "sql", sql,
					Integer.valueOf(actionForm.getRowcountperpage()), Integer
							.valueOf(actionForm.getCurrentpage()), "loadrichtext()");
			InterfaceStationIceList interfaceforresultsettolist = new InterfaceStationIceList();
			List<ViewOc_Station_ICE> list = interfaceforresultsettolist
					.ViewOc_News(pagebeanplus.getFormatqueryresultset());
			request.setAttribute("pagebeanplus", pagebeanplus);// 设置传递到页面的数据
			request.setAttribute("list", list);
			request.setAttribute("stationform", actionForm);
			System.out.println("list.size()" + list.size());
		return mapping.findForward("success");
	}

	public ActionForward admin_search_oc_station_ice(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		StationIcForm actionForm = (StationIcForm) form;
		String sql = "select ID,STATION_NAME,LONGITUDE,DIMENSIONALITY,TERRITORIAL_WATERS,WORK_TIME,DEPTH,OC_VOYAGE_ID,"
			+ "(SELECT OC_SEA_AREA from OC_SEA_AREA_ICE where id=TERRITORIAL_WATERS)OC_SEA_AREA,"
			+ "(SELECT VOYAGE_NAME FROM OC_VOYAGE_ICE WHERE ID=OC_VOYAGE_ID)VOYAGE_NAME from OC_STATION_ICE where DEL='OFF' ORDER BY id DESC";
	PageBeanPlus pagebeanplus = new PageBeanPlus("oracle", "sql", sql,
			Integer.valueOf(actionForm.getRowcountperpage()), Integer
					.valueOf(actionForm.getCurrentpage()), "loadrichtext()");
	InterfaceStationIceList interfaceforresultsettolist = new InterfaceStationIceList();
	List<ViewOc_Station_ICE> list = interfaceforresultsettolist
			.ViewOc_News(pagebeanplus.getFormatqueryresultset());
	request.setAttribute("pagebeanplus", pagebeanplus);// 设置传递到页面的数据
	request.setAttribute("list", list);
	request.setAttribute("stationform", actionForm);
	System.out.println("list.size()" + list.size());
		return mapping.findForward("success");
	}

	private String cut_tail(String str) {
		String cut = str;
		if (str.endsWith(",")) {
			cut = cut.substring(0, cut.length() - 1);
		}
		return cut;
	}
}
